﻿using System;
using System.Threading;

namespace Qplus.Infrastructure
{
    public class SendTask
    {

        public SendTask()
        {
        }

        public IAsyncResult BeginTestTask(string text, AsyncCallback asyncCallback, object state)
        {
            AsyncResult<string> asyncResult = new AsyncResult<string>(asyncCallback, state);
            ThreadPool.QueueUserWorkItem(DoTestTask, asyncResult);
            return asyncResult;
        }

        public string EndTestTask(IAsyncResult asyncResult)
        {
            return ((AsyncResult<string>)asyncResult).EndInvoke();
        }

        private void DoTestTask(Object asyncResult)
        {
            AsyncResult<string> ar = (AsyncResult<string>)asyncResult;
            try
            {
                Thread.Sleep(3000);
                ar.SetAsCompleted("测试完成", true);
            }
            catch (Exception e)
            {
                ar.SetAsCompleted(e, false);
            }
        }
    }
}
